package org.yaac.shared.editor;

import java.io.Serializable;
import java.util.List;

import org.yaac.shared.property.KeyInfo;

/**
 * @author Max Zhu (thebbsky@gmail.com)
 *
 */
public class EntityUpdateInfo implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * original key info, will never be changed
	 * 
	 * it is used to lookup the original entity from datastore
	 */
	private KeyInfo origKey;
	
	/**
	 * properties
	 */
	private List<PropertyUpdateInfo> changes;

	/**
	 * used by GWT
	 */
	@SuppressWarnings("unused")
	private EntityUpdateInfo() {}

	public EntityUpdateInfo(KeyInfo origKey, List<PropertyUpdateInfo> changes) {
		super();
		this.origKey = origKey;
		this.changes = changes;
	}
	
	public KeyInfo getOrigKey() {
		return origKey;
	}

	public List<PropertyUpdateInfo> getChanges() {
		return changes;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((changes == null) ? 0 : changes.hashCode());
		result = prime * result + ((origKey == null) ? 0 : origKey.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		EntityUpdateInfo other = (EntityUpdateInfo) obj;
		if (changes == null) {
			if (other.changes != null)
				return false;
		} else if (!changes.equals(other.changes))
			return false;
		if (origKey == null) {
			if (other.origKey != null)
				return false;
		} else if (!origKey.equals(other.origKey))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "EntityUpdateInfo [origKey=" + origKey + ", changes=" + changes + "]";
	}
}
